home *** CD-ROM | disk | FTP | other *** search
- ; Example 6: Interrupts.
-
- ; This example illustrates the use of an interrupt handler.
- ; To use it, you must turn on the "Use I/O Services" in the
- ; Options menu. You might want to run the program at
- ; "Fastest" speed with the Memory Display set to "Graphics".
-
- ; The program is really a combination of two previous
- ; examples, Example 3 and Example 5. The computer runs
- ; the program from example 3. However, when you type a
- ; character, an "interrupt handler" is called to process
- ; the character you typed. The processing is the same as
- ; what is done in Example 5. What the user of the program
- ; sees is the calculator program of Example 5, with the
- ; powers-of-three program from Example 3 running in the
- ; "background".
-
- ; For the best effect, run the program at Fastest speed in
- ; Graphics memory display mode. Type numbers separated by
- ; "+", "-", and "=".
-
- @PC 0 ; Make sure the PC contains 0, the starting location
- ; of the program.
-
- 1024# 0 ; Clear memory before loading
-
- @0 ; Start loading program at location 0
-
- lod-c 0 ; Set up the starting state
- sto num
- sto sum
- lod-c '+
- sto op
-
- inh calc ; Now, set up an interrupt handler at
- ; location calc. The computer will jump
- ; to this location to handle characters
- ; typed by the user.
-
- jmp bkg ; Jump to the "background" program at
- ; location "bkg"; this program is
- ; executed only if there is no input
- ; to be processed.
-
- ;------------------------------------------------------------
-
- @100 ; Load the calculator program at location 100
-
- calc: gtc ; Get a typed character from the input queue;
- ; there HAS to be one, since the compuer only
- ; jumps to this location when a character
- ; has been typed
- sto ch ; Save the typed character in "ch".
-
- sub-c '+ ; Test the typed character. If it is
- jmz do_op ; +, -, or =, then jump to "do_op".
- lod ch ; If it is a digit between 0 and 9,
- sub-c '- ; jump to "digit". Otherwise, it is
- jmz do_op ; illegal input; jump to "bad".
- lod ch
- sub-c '=
- jmz do_op
- lod ch
- sub-c '0
- jmn bad
- lod ch
- sub-c '9
- jmn digit
- jmz digit
-
- bad: lod-c '? ; Illegal input was typed. Output the
- ptc ; chatacter "?" followed by a space,
- lod-c 32 ; then return the calculator to its
- ptc ; starting state.
- jmp restart
-
- digit: lod ch ; A digit was typed.
- sub-c '0 ; Convert the character to the corresponding
- sto ch ; number and put that number back in ch.
- lod num ; Set num := 10*num + ch, effectively adding
- shl ; the typed digit onto the end of num.
- shl ; (10*num is computed by shifting num
- shl ; left three times, giving 8*num, and
- add num ; then adding num twice to that.)
- add num
- add ch
- sto num
- rti ; After processing the input digit, end
- ; the interrupt handler with a return-
- ; from-interrupt instruction
-
- do_op: lod num ; A "+", "-", or "=" was typed. Output
- ptu ; the current value of "num", and then
- lod op ; either add "num" to "sum" or subtract
- sub-c '+ ; it from "sum", depending on the
- jmz plus ; pending operation, "op".
- minus: lod sum
- sub num
- sto sum
- jmp fin_op
- plus: lod sum
- add num
- sto sum
- fin_op: lod-c 0 ; After the calculation, "num" is reset to 0
- sto num ; so that the user can start typing the
- lod ch ; next number. "Ch", the operation that
- ptc ; was just input, is printed.
- sub-c '= ; If "ch" is "+" or "-", then it is
- jmz equals ; stored in "op" to become the next
- lod ch ; pending operation.
- sto op
- rti ; Return-from-interrupt
-
- equals: lod sum ; If the typed character was "=", then
- ptu ; the final answer, "sum" is output,
- lod-c 32 ; followed by a space (ASCII 32) to
- ptc ; separate computations in the output list.
- jmp restart ; Then the calculator is returned to its
- ; starting state to get ready for the
- ; next computation.
-
- restart: lod-c 0 ; Restore the starting state of the calculator
- sto num ; and return from the interrupt handler
- sto sum
- lod-c '+
- sto op
- rti
- op: data ; Pending operation, that is, a "+" or "-"
- ; that will be performed after its second
- ; operand has been entered.
- ch: data ; Holds the character typed most recently
- ; by the user.
- num: data ; Holds a number as itis being typed by
- ; the user.
- sum: data ; Holds the result of any computation
- ; performed so far, while the user is
- ; typing the next number.
-
-
- ;------------------------------------------------------------
-
- @300 ; Load the "background" program at location 200
-
- bkg: lod-c 3 ; The program itself...
- sto N1
- lod-c 1
- sto ct1
-
- copy: lod ct1
- sto ct2
- sto ct
- lod-c N1
- sto src
- lod-c N2
- sto dest
- c1: lod-i src
- sto-i dest
- lod ct
- dec
- jmz sm
- sto ct
- lod src
- dec
- sto src
- lod dest
- dec
- sto dest
- jmp c1
-
- sm: lod ct2
- sto ct
- lod-c N2
- sto src
- lod-c N1
- sto dest
- lod-c 0
- sto carryQ
- s1: lod-i dest
- add-i src
- sto-i dest
- jmf cr1
- s2: lod-i dest
- add-i src
- sto-i dest
- jmf cr2
- s3: lod ct
- dec
- jmz copy
- sto ct
- lod src
- dec
- sto src
- lod dest
- dec
- sto dest
- jmp s1
-
- cr1: lod-c s2
- sto return
- jmp add1
-
- cr2: lod-c s3
- sto return
- jmp add1
-
- add1: lod ct
- sto aCt
- lod dest
- sto aDest
- a1: lod aDest
- dec
- sto aDest
- lod aCt
- dec
- sto aCt
- jmz advnc
- lod-i aDest
- add-c 1
- sto-i aDest
- jmf a1
- jmp-i return
- advnc: lod carryQ
- jmz adv1
- lod-i aDest
- add-c 1
- sto-i aDest
- jmp-i return
- adv1: lod ct1
- inc
- sto ct1
- lod-c 1
- sto-i aDest
- lod-c 1
- sto carryQ
- jmp-i return
-
- return:data ; Data locations used by the program in its
- ct1: data ; calculations
- ct2: data
- ct: data
- src: data
- dest: data
- aCt: data
- aDest: data
- carryQ:data
-
- @700 ; "N1" will refer to location 700
- N1: data
-
- @1000 ; "N2" will refer to location 1000
- N2: data
-